package com.example.blogserver.controller;

import com.example.blogserver.a_utils.MqConstants;
import com.example.blogserver.pojo.Article;
import com.example.blogserver.service.EditService;
import com.example.blogserver.a_utils.ResponseBodyMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings({"all"})
@RestController
@RequestMapping("/edit")
public class EditController {
    @Autowired
    private EditService editService;


    /**
     * 处理新增文章请求
     */
    @RequestMapping("/add")
    @Transactional//一旦发生异常就回滚操作
    public ResponseBodyMessage<Boolean> addArticle(HttpServletRequest request,
                                                   @RequestParam String titled,
                                                   @RequestParam String contentd) {
        return editService.addArticle(request, titled, contentd);
    }

    @RequestMapping("/isUpdate")
    public ResponseBodyMessage<Article> checkBlogId(HttpServletRequest request) {
        //获取queryString当中的blogId
        String blogId = request.getParameter("blogId");
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        if (blogId != null) {
            Article article = editService.getArticleByBlogId(blogId);
            article.setIsUpdate(1);
            responseBodyMessage.setStatus(1);
            responseBodyMessage.setMessage("修改文章");
            responseBodyMessage.setData(article);
        } else {
            responseBodyMessage.setStatus(-1);
            responseBodyMessage.setMessage("文章Id为空");
            responseBodyMessage.setData(null);
        }
        return responseBodyMessage;
    }
}
